package test;

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import test.table.DTable;
import test.table.GroupHeader;
import test.table.TableContent;

public class MyTestMain {
	public static void printDateGroup(){
		SimpleDateFormat formatmm =new SimpleDateFormat("mm");
		SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH");
		SimpleDateFormat formatfull =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String minutes =  formatmm.format( date) ;
		String newMinutes = "00";
		if ( Integer.parseInt(minutes)>30) 
			newMinutes = "30";
//		String newMinutes = minutes.substring(0,1)+0 ;
		System.out.println ( newMinutes ) 		;
		String prefixDate = format.format(date);
		String newStartDate = prefixDate + ":"+newMinutes+":00";
		System.out.println( prefixDate );
		int range=12;//hours
		int groupTime = 30;//minutes;
		Date curreentDate = null;
		List<String> starts = new ArrayList<String>();
		List<String> ends = new ArrayList<String>();
		try {
			curreentDate = formatfull.parse( newStartDate);
			int totalMinutes = range*60;
			int i=0;
			while(totalMinutes>0){
				long currentMillSeconds = curreentDate.getTime();
				long endMillSeconds = currentMillSeconds-i*groupTime*60*1000;
				long beginMillSeconds = currentMillSeconds - (i+1)*groupTime*60*1000;
				starts.add(formatfull.format( beginMillSeconds ));
				ends.add(formatfull.format( endMillSeconds ));
				totalMinutes=totalMinutes-groupTime;
				i++;
			}
			for(int j=starts.size()-1;j>0;j--){
				System.out.println( starts.get(j) + " -- "+ends.get(j));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		SimpleDateFormat formatMoth =new SimpleDateFormat("yyyy-MM");
		System.out.println(  formatMoth.format(new Date())) ;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		System.out.println(  89/10);
//		printDateGroup();
		final JTable table = createTable();
		JFrame frame = new JFrame("锟斤拷锟斤拷JTable");
		frame.setSize(600, 350);
		frame.setLayout(new GridBagLayout());
		
		frame.add(new JScrollPane(table),new GBC(0,0).setWeight(100,100).setFill(GBC.BOTH));
		JButton printButton = new JButton("Print");
		printButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				try {
					table.print();
				} catch (java.awt.print.PrinterException e) {
					e.printStackTrace();
				}
			}
		});	
		
		frame.add(printButton,new GBC(0,1));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	public static JTable createTable() {
		TableContent tableContent = new TableContent();
		 
		Vector rowData = new Vector();
		rowData.add("1");//1
		rowData.add("锟斤拷械锟斤拷");//2
		rowData.add("");//3
		rowData.add("");//4
		rowData.add("");//5
		rowData.add("");//6
		rowData.add("720000");//7
		rowData.add("");//8
		rowData.add("");//9
		rowData.add("");//10
		rowData.add("");//11
		rowData.add("720000");//12
		rowData.add("");//13
		rowData.add("628848");//14
		rowData.add("");//15
 
		for(int i=0;i<rowData.size();i++){
			tableContent.append(rowData.get(i));
		}
		
		List<GroupHeader> list=new ArrayList<GroupHeader>();
		list.add(new GroupHeader("锟斤拷失锟斤拷(%)",7,10));
		JTable table =  DTable.create(
				tableContent
				,new Object[]{
				"序号",//1
				"单位",//2
				"9.1表",//3
				"10.1表",//4
				"差数",//5
				"倍率",//6
				"实表数",//7
				"线损",//8
				"变",//9
				"增",//10
				"总损",//11
				"计费电量",//12
				"单价",//13
				"应交电费",//14
				"备注"//15

				}
				,list);
//		DefaultTableModel model = (DefaultTableModel) table.getModel();

//		table.setModel(model);
		return table;
		
	}
}
